package com.cenwangkeji.resident.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.bean.LoginBean;
import com.cenwangkeji.resident.presenter.activity.LoginPresenter;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.SharePreferenceUtils;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

//登录界面
public class LoginActivity extends BaseActivity implements LoginPresenter.I{

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    private LoginPresenter mPresenter = new LoginPresenter(this);

    @Override
    public int setLayoutId() {
        return R.layout.activity_login;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn, R.id.tv_forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn:
                login();
                break;
            case R.id.tv_forget:
                startActivity(new Intent(this,ForgetPasswordActivity.class));
                break;
        }
    }

    //登陆
    private void login(){
        String phone = mEtPhone.getText().toString().trim();
        String pass = mEtPassword.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            MyToast.show("帐号不能为空");
        }else  if(TextUtils.isEmpty(pass)){
            MyToast.show("密码不能为空");
        }else {
            mPresenter.login(phone, pass);

        }
    }


    //登录成功
    @Override
    public void loginSuccess(LoginBean bean) {
        if (bean.getGrjbxx()!= null){
            MyToast.show("登陆成功");
            SharePreferenceUtils.putBoolean(this, Constant.is_login, true);//登录标识
            SharePreferenceUtils.putString(this, Constant.token, bean.getGrjbxx().getToken());//token
            SharePreferenceUtils.putInt(this, Constant.user_id, bean.getGrjbxx().getId());// id
            SharePreferenceUtils.putString(this, Constant.phone, bean.getGrjbxx().getBrdh());//电话
            SharePreferenceUtils.putString(this, Constant.username, bean.getGrjbxx().getXm());//姓名
            SharePreferenceUtils.putInt(this, Constant.doctor_id, bean.getGrjbxx().getCreateBy());//属于医生id  createBy
            JPushInterface.setAlias(this, 1,bean.getGrjbxx().getBrdh());//设置别名
            Set<String> set = new HashSet<>();
            set.add("");
            //JPushInterface.setTags(this, 2, );
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }else {
            MyToast.show(bean.getMsg());
        }
    }
}
